using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using PointOfSale.Global;
using PointOfSale.Class;

namespace PointOfSale.Configuration.Department
{
	/// <summary>
	/// Summary description for frmAddDepartment.
	/// </summary>
	public class frmAddDepartment : System.Windows.Forms.Form
	{
		private Joaqs.UI.XpGroupBox xpGroupBox1;
		private Newco.Controls.LabelLine labelLine1;
		private Joaqs.UI.InputTextBox txtDepartmentName;
		private Joaqs.UI.XpButton btnOK;
		private Joaqs.UI.XpButton btnCancel;

		//
		//Variable for manipulate with database
		//
		private System.Data.SqlClient.SqlCommand sqlCommand;
		private System.Data.DataSet dataSet;
		private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmAddDepartment()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.xpGroupBox1 = new Joaqs.UI.XpGroupBox();
			this.txtDepartmentName = new Joaqs.UI.InputTextBox();
			this.labelLine1 = new Newco.Controls.LabelLine();
			this.btnOK = new Joaqs.UI.XpButton();
			this.btnCancel = new Joaqs.UI.XpButton();
			this.xpGroupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// xpGroupBox1
			// 
			this.xpGroupBox1.Controls.Add(this.txtDepartmentName);
			this.xpGroupBox1.Controls.Add(this.labelLine1);
			this.xpGroupBox1.Location = new System.Drawing.Point(8, 8);
			this.xpGroupBox1.Name = "xpGroupBox1";
			this.xpGroupBox1.Size = new System.Drawing.Size(272, 56);
			this.xpGroupBox1.TabIndex = 0;
			this.xpGroupBox1.TabStop = false;
			this.xpGroupBox1.Text = "Enter Department";
			// 
			// txtDepartmentName
			// 
			this.txtDepartmentName.Location = new System.Drawing.Point(104, 24);
			this.txtDepartmentName.Name = "txtDepartmentName";
			this.txtDepartmentName.Size = new System.Drawing.Size(160, 20);
			this.txtDepartmentName.TabIndex = 1;
			this.txtDepartmentName.Text = "";
			// 
			// labelLine1
			// 
			this.labelLine1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(70)), ((System.Byte)(213)));
			this.labelLine1.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(208)), ((System.Byte)(208)), ((System.Byte)(191)));
			this.labelLine1.Location = new System.Drawing.Point(8, 24);
			this.labelLine1.Name = "labelLine1";
			this.labelLine1.Size = new System.Drawing.Size(88, 23);
			this.labelLine1.TabIndex = 0;
			this.labelLine1.Text = "Department Name";
			// 
			// btnOK
			// 
			this.btnOK.AdjustImageLocation = new System.Drawing.Point(0, 0);
			this.btnOK.BtnShape = Joaqs.UI.emunType.BtnShape.Rectangle;
			this.btnOK.BtnStyle = Joaqs.UI.emunType.XPStyle.Default;
			this.btnOK.Location = new System.Drawing.Point(120, 72);
			this.btnOK.Name = "btnOK";
			this.btnOK.TabIndex = 1;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.AdjustImageLocation = new System.Drawing.Point(0, 0);
			this.btnCancel.BtnShape = Joaqs.UI.emunType.BtnShape.Rectangle;
			this.btnCancel.BtnStyle = Joaqs.UI.emunType.XPStyle.Default;
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(208, 72);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 2;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// frmAddDepartment
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(288, 102);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.xpGroupBox1);
			this.Name = "frmAddDepartment";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Add Department";
			this.Load += new System.EventHandler(this.frmAddDepartment_Load);
			this.Activated += new System.EventHandler(this.frmAddDepartment_Activated);
			this.xpGroupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

			this.xpGroupBox1.Text = clsGlobal.g_Hashtable["EnterDepartment"].ToString();
			this.labelLine1.Text = clsGlobal.g_Hashtable["AddDepartmentName"].ToString();
			this.btnOK.Text = clsGlobal.g_Hashtable["AddDepartmentbtnOK"].ToString();
			this.btnCancel.Text = clsGlobal.g_Hashtable["AddDepartmentbtnCancel"].ToString();
			this.Text = clsGlobal.g_Hashtable["AddDepartmentFormName"].ToString();

			this.xpGroupBox1.Text = clsGlobal.g_Hashtable["EnterDepartment"].ToString();
			this.labelLine1.Text = clsGlobal.g_Hashtable["AddDepartmentName"].ToString();
			this.btnOK.Text = clsGlobal.g_Hashtable["AddDepartmentbtnOK"].ToString();
			this.btnCancel.Text = clsGlobal.g_Hashtable["AddDepartmentbtnCancel"].ToString();
			this.Text = clsGlobal.g_Hashtable["AddDepartmentFormName"].ToString();
		}
		#endregion

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			string strDepartmentName = this.txtDepartmentName.Text;

			string SelectCommand = "SELECT DepartmentID, DepartmentName from tblDepartment";
			SqlCommand _sqlCommand = new SqlCommand(SelectCommand,clsGlobal.g_DataConn);
			SqlDataReader dr = _sqlCommand.ExecuteReader();

			bool flag = true;

			while(dr.Read())
			{
				string strTempDepartmentName = dr["DepartmentName"].ToString();
				if(String.Compare(strTempDepartmentName, strDepartmentName, true) == 0)
				{
					flag = false;
				}
			}
			dr.Close();

			if(strDepartmentName.Length == 0)
			{
				MessageBox.Show(clsGlobal.g_Hashtable["msgDepartmentEnterName"].ToString());
				this.ActiveControl = this.txtDepartmentName;
			}
			else
			{
				if(!flag)
				{
					MessageBox.Show(clsGlobal.g_Hashtable["msgDepartmentExistedName"].ToString());
					this.ActiveControl = this.txtDepartmentName;
				}
				else
				{
					clsDepartment dept = new clsDepartment();
					dept.Name = strDepartmentName;
					dept.Add();
					this.dataSet.Clear();

					this.ActiveControl = this.txtDepartmentName;
					this.txtDepartmentName.Text = "";

					frmDepartmentMaster.Call delInstance = new frmDepartmentMaster.Call(frmDepartmentMaster.CallMethod);

					delInstance(strDepartmentName);
				}
			}
		}

		private void frmAddDepartment_Load(object sender, System.EventArgs e)
		{
			this.sqlCommand = new SqlCommand();
			this.sqlCommand.Connection = clsGlobal.g_DataConn;
			
			this.sqlDataAdapter = new SqlDataAdapter();
			this.sqlDataAdapter.SelectCommand = this.sqlCommand;

			this.dataSet = new DataSet();
			this.dataSet.CaseSensitive = true;
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void frmAddDepartment_Activated(object sender, System.EventArgs e)
		{
			this.ActiveControl = this.txtDepartmentName;
		}
	}
}
